﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using suirui.ZhuMu.Common;
using suirui.ZhuMu.Model;
using suirui.ZhuMu.Entity;
using suirui.ZhuMu.Entity.Entities;
using suirui.ZhuMu.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using IdentityModel;
using suirui.ZhuMu.WebCore;
using suirui.ZhuMu.Framework;

namespace suirui.ZhuMu.MJGames.Controllers
{
    public class UserController : BaseController
    {
        private readonly IUserService _UserSerivce;

        public UserController(IUserService userSerivce)
        {
            this._UserSerivce = userSerivce ?? throw new ArgumentNullException(nameof(userSerivce));
        }

        /// <summary>
        /// 获取用户信息列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<PageData<User>>> GetUserList(GetListByUserDto paramsDto)
        {
            return _UserSerivce.GetUserList(paramsDto);
        }

        /// <summary>
        /// 通过Id获取单个用户的信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<User>> GetUserById(BaseDto user)
        {
            if (user.Id <= 0)
            {
                return new APIResult<User>() { Code = ErrorCode.FormatError, Message = "Id不能小于0" };
            }
            var result = await _UserSerivce.GetUserById(user.Id);
            var userData = result.Data;
            if (userData != null)
            {
                return new APIResult<User>() { Data = userData };
            }
            return new APIResult<User>() { Code = ErrorCode.ResultError, Message = result.Message };
        }

        /// <summary>
        /// 通过Email获取单个用户的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<User>> GetUserByEmail(string Email)
        {
            if (string.IsNullOrWhiteSpace(Email))
            {
                return new APIResult<User>() { Code = ErrorCode.FormatError, Message = "电子邮件不能为空" };
            }
            return await _UserSerivce.GetUserByEmail(Email);
        }

        /// <summary>
        /// 注册用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[SignParamFilter]
        [AllowAnonymous]
        [PreventDuplicationRequest]
        public async Task<APIResult> AddUser(UserDto user)
        {
            if (!CheckInvalid(user))
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "用户参数不能为null" };
            }
            var result = new APIResult();
            var mail = DESUtils.DecryptString(user.Email);
            if (!string.IsNullOrEmpty(mail))
            {
                if (!Util.IsEmail(mail))
                {
                    result.Message = "电子邮箱的格式不正确！";
                    result.Code = ErrorCode.EmailInvalid;
                    return result;
                }
            }
            if (_UserSerivce.IsExistReMail(user.Email).Data)
                return new APIResult<string>() { Code = ErrorCode.EmailExist, Message = $"该电子邮箱已被注册，请重新输入。" };

            return await _UserSerivce.AddUser(user);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        [HttpPut]
        public async Task<APIResult> UpdateUser(User user)
        {
            if (!CheckInvalid(user))
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "用户参数不能为null" };
            }
            var result = new APIResult();
            var mail = DESUtils.DecryptString(user.Email);
            if (!string.IsNullOrEmpty(mail))
            {
                if (!Util.IsEmail(mail))
                {
                    result.Message = "电子邮箱的格式不正确！";
                    result.Code = ErrorCode.EmailInvalid;
                    return result;
                }
            }
            return await _UserSerivce.UpdateUser(user);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="Id"></param>
        [HttpDelete]
        public async Task<APIResult> DeleteUser(long Id)
        {
            if (Id <= 0)
            {
                return new APIResult() { Code = ErrorCode.FormatError, Message = "Id不能小于或等于0" };
            }
            return await _UserSerivce.DeleteUser(Id);
        }


        private bool CheckInvalid(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            return true;
        }

    }
}
